package com.zelin.shop.controller;

import com.zelin.shop.entity.ItemCatEntity;
import com.zelin.shop.service.ItemCatService;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 商品类目
 *
 * @author hbxfwf
 * @email hbxfwf@sina.com
 * @date 2021-10-22 16:59:59
 */
@RestController
@RequestMapping("shop/itemcat")
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;


    //1. 查询一级分类
    @GetMapping("/findItemCat1")
    public R findItemCat1(){
        List<ItemCatEntity> itemCatEntities = itemCatService.findItemCat1();
        return R.ok().put("list",itemCatEntities);
    }
    //2. 根据父id查询其下的子分类列表
    @GetMapping("/findParentById/{newId}")
    public R findParentById(@PathVariable Long newId){
        return R.ok().put("list",itemCatService.findParentById(newId));
    }
    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("manager:itemcat:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = itemCatService.queryPage(params);

        return R.ok().put("page", page);
    }

    //查询所有分类
    @GetMapping("/findItemCats")
    public R findItemCats(){
        return R.ok().put("itemCats",itemCatService.list());
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("manager:itemcat:info")
    public R info(@PathVariable("id") Long id){
		ItemCatEntity itemCat = itemCatService.getById(id);

        return R.ok().put("itemCat", itemCat);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("manager:itemcat:save")
    public R save(@RequestBody ItemCatEntity itemCat){
		itemCatService.save(itemCat);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("manager:itemcat:update")
    public R update(@RequestBody ItemCatEntity itemCat){
		itemCatService.updateById(itemCat);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("manager:itemcat:delete")
    public R delete(@RequestBody Long[] ids){
		itemCatService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
